package cn.schoolwow.quickdao.module.database.dml.json.flow.save;

import cn.schoolwow.quickdao.module.database.dml.json.service.insert.InsertJSONArrayCompositeBusiness;
import cn.schoolwow.quickdao.module.database.dml.json.service.update.UpdateJSONArrayCompositeBusiness;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;

public class SaveJSONArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        int effect = 0;
        //不存在则插入
        {
            JSONArray notExistJSONArray = (JSONArray) flowContext.getData("notExistJSONArray");
            if(null!=notExistJSONArray&&!notExistJSONArray.isEmpty()){
                flowContext.startFlow(new InsertJSONArrayCompositeBusiness())
                        .putTemporaryData("instances", notExistJSONArray)
                        .execute();
                effect += (Integer)flowContext.checkData("effect");
            }
        }

        //存在则更新
        {
            JSONArray existJSONArray = (JSONArray) flowContext.getData("existJSONArray");
            if(null!=existJSONArray&&!existJSONArray.isEmpty()){
                flowContext.startFlow(new UpdateJSONArrayCompositeBusiness())
                        .putTemporaryData("instances", existJSONArray)
                        .execute();
                effect += (Integer)flowContext.checkData("effect");
            }
        }

        flowContext.putData("effect", effect);
    }

    @Override
    public String name() {
        return "保存JSONArray";
    }

}
